package com.dengdai.applibrary.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.ActivityInterface;
import com.dengdai.applibrary.interfaces.FragmentInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.utils.OperationUtils;
import com.dengdai.applibrary.widgets.progres.CustomLoadingDialoge;
import com.ypy.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * @说明 Fragment基础
 * @作者 LY
 * @时间 16/5/24 11:48
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseFragment extends Fragment implements FragmentInterface {
    /**
     * 动画类型
     */
    private int mAnimationType = ANIMATION_NONE;
    /**
     * 业务操作
     */
    private OperationUtils mOperation = null;
    /**
     * 加载对话款
     */
    private CustomLoadingDialoge customLoadingDialoge;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        mOperation = new OperationUtils(getActivity());
        business();
    }

    @Override
    public void finishActivity() {
        getActivity().finish();
        switch (mAnimationType) {
            case ActivityInterface.ANIMATION_LEFT_RIGHT:
                getActivity().overridePendingTransition(0, BaseViewUtils.gainResId(getActivity(), BaseViewUtils.ANIM, ViewInfo.SLIDE_RIGHT_OUT));
                break;
            case ActivityInterface.ANIMATION_TOP_BOTTOM:
                getActivity().overridePendingTransition(0, BaseViewUtils.gainResId(getActivity(), BaseViewUtils.ANIM, ViewInfo.PUSH_UP_OUT));
                break;
            case ActivityInterface.ANIMATION_FADE_IN_OUT:
                getActivity().overridePendingTransition(0, BaseViewUtils.gainResId(getActivity(), BaseViewUtils.ANIM, ViewInfo.FADE_OUT));
                break;
        }
        mAnimationType = ANIMATION_NONE;
    }

    /**
     * 获取界面跳转对象
     *
     * @return
     */
    protected OperationUtils getOperation() {
        if (mOperation == null) {
            mOperation = new OperationUtils(getActivity());
        }
        return mOperation;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResID(), container, false);
        ButterKnife.bind(this, view);
        initData(view);
        return view;
    }

    @Override
    public void startLoadingDialoge() {
        startLoadingDialoge(getResources().getString(BaseViewUtils.gainResId(getActivity(), BaseViewUtils.STRING, ResIdName.LOADING)));
    }

    @Override
    public void startLoadingDialoge(String message) {
        if (customLoadingDialoge == null) {
            customLoadingDialoge = new CustomLoadingDialoge(getActivity(), message);
        }
        customLoadingDialoge.show();
    }

    @Override
    public void stopLoadingDialoge() {
        if (customLoadingDialoge != null) {
            customLoadingDialoge.dismiss();
            customLoadingDialoge = null;
        }
    }

    /**
     * 消息监听
     *
     * @param object 消息
     */
    protected void onEvent(Object object) {
        event(object);
    }

    @Override
    public void onPause() {
        super.onPause();
        pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        destroy();
        //关闭消息监听
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void event(Object object) {

    }
}
